package com.gpoint.rdp.door.campusPid.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gpoint.rdp.base.core.util.string.StringUtil;
import com.gpoint.rdp.base.core.entity.ResultMessage;
import com.gpoint.rdp.base.framework.page.PageList;
import com.gpoint.rdp.base.framework.page.PageResult;
import com.gpoint.rdp.api.base.query.QueryFilter;
import com.gpoint.rdp.base.web.controller.GenericController;
import com.gpoint.rdp.base.web.json.PageJson;
import com.gpoint.rdp.base.web.util.RequestUtil;
import com.gpoint.rdp.door.campusPid.service.CampusPidService;
import com.gpoint.rdp.door.doorPermission.persistence.entity.PermissTypeAndUserPo;
import com.gpoint.rdp.door.user.persistence.entity.CampusCardPo;
import com.gpoint.rdp.door.campusPid.service.CampusPidQueryService;
import com.gpoint.rdp.door.TimeSegment.persistence.entity.TimeSegPo;
import com.gpoint.rdp.door.campusPid.persistence.dao.CampusPidQueryDao;
import com.gpoint.rdp.door.campusPid.persistence.entity.CampusPidPo;
import com.gpoint.rdp.base.core.util.BeanUtils;
import com.gpoint.rdp.base.core.util.JacksonUtil;

import java.util.ArrayList;
import java.util.List;

// TODO: Auto-generated Javadoc
/**
 * 身份表 控制类
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-11-27 14:41:14
 *</pre>
 */
@Controller
@RequestMapping("/rdp/campusPid/campusPid/")
public class CampusPidController extends GenericController{
	
	/** The campus pid service. */
	@Resource
	private CampusPidService campusPidService;
	
	/** The campus pid query service. */
	@Resource
	private CampusPidQueryService campusPidQueryService;
	

	
	/**
	 * List.
	 * matain list view.
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("list")
	 public ModelAndView list(HttpServletRequest request,HttpServletResponse response) throws Exception{
		 ModelAndView mv=getAutoView();
	  return  mv;
	  
	 }
	
	
	/**
	 * List.
	 *
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("campusPidListView")
	 public ModelAndView campusPidListView(HttpServletRequest request,HttpServletResponse response) throws Exception{
		  String jsp="/rdp/campusPid/campusPidList2.jsp"; 
		  ModelAndView mv= new	  ModelAndView(jsp);
		  return mv;
		
	  
	 }
	
	/**
	 * Permission user list.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("permissionUserList")
	public @ResponseBody PageJson permissionUserList(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			
			PageList<PermissTypeAndUserPo> permissTypeAndUserList = null;
			List<PermissTypeAndUserPo> list = new ArrayList<PermissTypeAndUserPo>();
			PageResult pageResult = new PageResult();
			permissTypeAndUserList = new PageList<PermissTypeAndUserPo>(list, pageResult);			
			return new PageJson(permissTypeAndUserList);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	

	@RequestMapping("getData")
	public @ResponseBody List<CampusPidPo> getTreeData(HttpServletRequest request, HttpServletResponse reponse)
			throws Exception {
		try {
			
			List<CampusPidPo> list=       this.campusPidQueryService.getRecord();
			if (BeanUtils.isEmpty(list)) {
				list = new ArrayList<CampusPidPo>();
			}

			return list;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return null;
		}
	}
	
	
	
	/**
	 * 【身份表】列表(分页条件查询)数据.
	 *
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("listJson")
	public @ResponseBody PageJson listJson(HttpServletRequest request,HttpServletResponse reponse) throws Exception{
		QueryFilter queryFilter = getQuerFilter(request);
		try{
			String listData = campusPidQueryService.query(queryFilter);
			PageList<CampusPidPo> campusPidList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<CampusPidPo> list = CampusPidPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				campusPidList = new PageList<CampusPidPo>(list, pageResult);
			}
			
			return new PageJson(campusPidList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	/**
	 * Arr str to sql str.
	 *
	 * @param arrStr the arr str
	 * @return the string
	 */
	private String arrStrToSqlStr(String arrStr) {
		String sqlStr = "";
		StringBuffer sb = new StringBuffer("(");
		String[] strArr = arrStr.split(",");
		for (int i = 0; i < strArr.length; i++) {
			sb.append("'").append(strArr[i]).append("'");
			if (i != strArr.length - 1) {
				sb.append(",");
			} else {
				sb.append(")");
			}
		}
		sqlStr = sb.toString();
		return sqlStr;
	}
	
	/**
	 * List by ids.
	 *
	 * @param ids the ids
	 * @param request the request
	 * @param reponse the reponse
	 * @return the page json
	 * @throws Exception the exception
	 */
	@RequestMapping("listByIds")
	public @ResponseBody PageJson listByIds(@RequestParam String ids, HttpServletRequest request,
			HttpServletResponse reponse) throws Exception {
		QueryFilter queryFilter = getQuerFilter(request);
		PageList<CampusPidPo> campusCardList = null;
		if (StringUtil.isNotEmpty(ids)) {
			String sqlStr = arrStrToSqlStr(ids);
			queryFilter.addParamsFilter("whereSql", "pid in " + sqlStr);
		} else {
			List<CampusPidPo> list = new ArrayList<CampusPidPo>();
			PageResult pageResult = new PageResult();
			campusCardList = new PageList<CampusPidPo>(list, pageResult);
			return new PageJson(campusCardList);
		}		
		try {
			String listData = campusPidQueryService.query(queryFilter);
			PageList<CampusPidPo> campusPidList = null;
			if(JacksonUtil.isJsonObject(listData)){
				List<CampusPidPo> list = CampusPidPo.fromJsonArrayString(JacksonUtil.getString(listData, "data"));
				PageResult pageResult = PageResult.fromJson(JacksonUtil.getString(listData, "pageResult"));
				campusPidList = new PageList<CampusPidPo>(list, pageResult);
			}
			
			return new PageJson(campusPidList);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return new PageJson();
		}
	}
	
	
	/**
	 * 编辑【身份表】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("edit")
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		CampusPidPo campusPid=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = campusPidQueryService.get(id);
			campusPid = CampusPidPo.fromJsonString(data);
		}

		return getAutoView().addObject("campusPid", campusPid).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 编辑【身份表】信息页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("flowEdit")
	public ModelAndView flowEdit(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		CampusPidPo campusPid=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = campusPidQueryService.get(id);
			campusPid = CampusPidPo.fromJsonString(data);
		}
		return getAutoView().addObject("campusPid", campusPid).addObject("returnUrl", preUrl);
	}
	
	/**
	 * 【身份表】明细页面.
	 *
	 * @param request the request
	 * @param response the response
	 * @return the model and view
	 * @throws Exception the exception
	 */
	@RequestMapping("get")
	public ModelAndView get(HttpServletRequest request,HttpServletResponse response) throws Exception{
		String preUrl= RequestUtil.getPrePage(request);
		String id=RequestUtil.getString(request, "id");
		CampusPidPo campusPid=null;
		if(StringUtil.isNotEmpty(id)){
			String data = null;
			data = campusPidQueryService.get(id);
			campusPid = CampusPidPo.fromJsonString(data);
		}
		return getAutoView().addObject("campusPid", campusPid).addObject("returnUrl", preUrl);
	}
	
	/**
	 *  
	 * 保存【身份表】信息.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("save")
	public void save(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			String json = RequestUtil.getString(request, "json");
			campusPidService.save(json);
			message=new ResultMessage(ResultMessage.SUCCESS, "保存身份表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "对身份表操作失败,"+e.getMessage());
			logger.error("对身份表操作失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	
	/**
	 *  批量删除【身份表】记录.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws Exception the exception
	 */
	@RequestMapping("remove")
	public void remove(HttpServletRequest request,HttpServletResponse response) throws Exception{
		ResultMessage message=null;
		try {
			//获得待删除的id
			String[] ids=RequestUtil.getStringAryByStr(request, "id");
			campusPidService.deleteByIds(ids);
			message=new ResultMessage(ResultMessage.SUCCESS, "删除身份表成功");
		} catch (Exception e) {
			message=new ResultMessage(ResultMessage.FAIL, "删除身份表失败，" + e.getMessage());
			logger.error("删除身份表失败，" + e.getMessage(),e);
		}
		writeResultMessage(response.getWriter(), message);
	}
	

}